package com.iad.controller.command
{
  import com.adobe.cairngorm.commands.Command;
  import com.adobe.cairngorm.control.CairngormEvent;
  import com.iad.controller.event.*;
  import com.iad.controller.service.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  import com.iad.view.component.*;
  import com.iad.view.form.*;
  import com.iad.view.util.ascb.util.*;
  
  import mx.collections.ArrayCollection;
  
  public class PlaylistCommand implements Command
  {
    [Bindable]
    private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
 
    public function execute(event:CairngormEvent):void {
      switch ( event.type ) {
        case PlaylistEvent.GET_PLAYLIST:
          getPlaylist();
        break;
        case PlaylistEvent.GET_PLAYLIST_FROM_NEW_SITE:
          getPlaylistFromNewSite();
        break;
        case PlaylistEvent.GET_SITE_PLAYLISTS:
          getSitePlaylists();
        break;
        case PlaylistEvent.DELETE_SONG:
          deleteSong(event as PlaylistEvent);
        break;
     }
    }
    
    
    private function deleteSong(event:PlaylistEvent):void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.DELETE_SONG_RESULT, deleteSongOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.deleteSong((event.object as Song).id);
    }
    
    
    protected function deleteSongOnResult(event:ServiceResultEvent):void {
      trace("PlaylistCommand::deleteSongOnResult()");
      getPlaylist();
    }
    
    
    private function onError(event:ServiceResultEvent):void {
      trace("error: " + event);
    }
    
     
    private function getSitePlaylists():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_SITE_PLAYLISTS_RESULT, getSitePlaylistsOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSitePlaylists(model.currentSiteId);
    }
    
     
    protected function getSitePlaylistsOnResult(event:ServiceResultEvent):void {
      var playlists:ArrayCollection = event.result as ArrayCollection;
      trace("PlaylistCommand::getSitePlaylistsOnResult(): size: " + playlists.length);
      for each(var playlist:Playlist in playlists) {
        trace(playlist);
        //model.discographyItems.addItem(playlist);
      }
      model.playlists = playlists;
    }
    
  
    private function getPlaylistFromNewSite():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_PLAYLIST_FROM_NEW_SITE_RESULT, getPlaylistFromNewSiteOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getPlaylistFromNewSite(model.currentSiteId);
    }
    
     
    protected function getPlaylistFromNewSiteOnResult(event:ServiceResultEvent):void {
      var playlist:Playlist = event.result as Playlist;
      model.currentPlaylistId = playlist.id;
      model.currentSite.currentPlaylistId = playlist.id;
    }
        
        

    private function getPlaylist():void {
      var service:SiteService = new SiteService();
      service.addEventListener(ServiceResultEvent.GET_PLAYLIST_RESULT, getPlaylistOnResult, false, 0, false);
      service.addEventListener(ServiceErrorEvent.ERROR, onError, false, 0, true);
      service.getSongsInPlaylist(model.currentSite.currentPlaylistId);
    }
    
     
    protected function getPlaylistOnResult(event:ServiceResultEvent):void {
      var songs:ArrayCollection = event.result as ArrayCollection;
      trace("PlaylistCommand::getPlaylistOnResult(): size: " + songs.length);
      for each(var song:Song in songs) {
        trace(song);
      }
      model.songsArray = songs;
      model.app.mp3Player.songsList.selectedIndex = 0;
      model.currentPlaylistSiteName = model.currentSite.name
      model.currentPlaylistSiteId = model.currentSite.id
      if (songs != null && songs.length > 0) {
        model.currentSong = songs.getItemAt(0) as Song;
      }
    }
        
        
    
    private function setDefaults():void {
    }
  } 
}
    